package gov.pbc.nn.kjgl.software.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.kjgl.entity.Project;
import gov.pbc.nn.kjgl.entity.ProjectMember;
import gov.pbc.nn.kjgl.entity.ProjectProblem;

@Repository
public class MyProjectProblemDao extends AbstractHibernateDao {

	/**
	 * 问题表数据
	 * 
	 * @param page
	 * @param projectId
	 */
	public void getProjectProblem(Page<ProjectProblem> page,Criteria criteria,String Id) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, ProjectProblem.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.eq("projectId", Id));
		query.add(Restrictions.eq("person", ContextHolder.getLoginUserName()));
		this.pagingQuery(page,query);
	}

//	/**
//	 * 当前登陆人所参与的项目
//	 * 
//	 * @return
//	 */
//	public List<Project> getProject() {
//		String hql = "from ProjectMember where invalid=false and username=:username";
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("username", ContextHolder.getLoginUserName());
//		List<ProjectMember> ls = this.query(hql, map);
//		List<Project> list = new ArrayList<>();
//		if (ls != null) {
//			String hql1 = "from Project where invalid=false and id=:projectId and state=10";
//			Map<String, Object> map1 = new HashMap<String, Object>();
//			for (ProjectMember projectMember : ls) {
//				String projectId = projectMember.getProjectId();
//				map1.put("projectId", projectId);
//				List<Project> lists = this.query(hql1, map1);
//				if (lists.size() >0) {
//					list.add(lists.get(0));
//				}
//			}
//			return list;
//		}
//		return null;
//	}

	/**
	 * 已完成
	 * 
	 * @param list
	 */
	public void finshWork(List<String> list) {
		for (String string : list) {
			ProjectProblem pro = new ProjectProblem();
			pro.setFinishTime(new Date());
			pro.setProgress(100);
			this.update(pro);
		}
	}

	public void saveWork(List<ProjectProblem> list) {
		for (ProjectProblem projectProblem : list) {
			EntityState s = EntityUtils.getState(projectProblem);
			if (s == EntityState.MODIFIED) {
				this.update(projectProblem);
			}
		}
	}
}
